'use strict';

const os = require('os');
class Olt {
  constructor(options) {
    this.options = options;
  }

  async TestConnection() {
    return new Promise((resolve, reject) => {
      const socketclient = net.createConnection(
        {
          host: this.options.host,
          port: this.options.tPort || 23,
        },
        function() {
          // console.log('connected');
        }
      );
      let step = 0;
      socketclient.on('data', function(data) {
        const info = data.toString();
        if (step == 2) {
          if (info.indexOf('Login Failed') != -1) {
            // console.log('登录失败');
            socketclient.destroy();
            resolve(false);
          } else {
            // console.log('登录登录成功');
            socketclient.destroy();
            resolve(true);
          }
        }
        if (info.indexOf('Login') != -1) {
          step++;
          socketclient.write(`${options.username}\r\n`);
        }
        if (info.indexOf('Password') != -1) {
          step++;
          socketclient.write(`${options.password}\r\n`);
        }
        // console.log(`receive data : ${data}`);
      });

      socketclient.on('close', function() {
        // console.log('close');
      });

      socketclient.on('error', function(err) {
        // console.log(err);
        reject(err);
      });
    });
  }

  getTargetIndex(arr, target) {
    const reg = new RegExp(target, 'ig');
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].match(reg)) {
        return i;
      }
    }
  }

  getonuArr(arr) {
    const result = [];
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].match(/OnuIndex/ig) ||
        arr[i].match(/-{2,}/ig) ||
        arr[i].match(/ONU Number/ig) ||
        arr[i].match(/#/ig)
      ) {
        continue;
      } else {
        result.push(arr[i]);
      }
    }
    return result;
  }
  getOnuObj() {
    return {
      PONindex: '',
      ONUindex: '',
      state: '',
      onuprofile: '',
      mode: '',
      info: '',
      Temperature: '',
      Powerfeedvoltage: '',
      Laserbiascurrent: '',
      TXpwr: '',
      RXpwr: '',
      lineprofile: '',
      VendorID: '',
      Version: '',
      SN: '',
      Operatestatus: '',
      EquipmentID: '',
      OMCCVersion: '',
      Securitycapability: '',
      Model: '',
      Survivaltime: '',
      TotalGemPortNum: '',
      SysUpTime: '',
      Description: '',
      Mainsoftwareversion: '',
      Standbysoftwareversion: '',
      TotalTCONTnumber: '',
      TotalUNInumber: '',
      EthernetUNInumber: '',
      TenGEnumber: '',
      GEnumber: '',
      FEnumber: '',
      CESUNInumber: '',
      POTSUNInumber: '',
      VideoUNInumber: '',
      WIFIUNInumber: '',
      XDSLUNInumber: '',
      IPhostnumber: '',
      IPv6hostnumber: '',
      VEIPnumber: '',
      OperationId: '',
      CTCspcversion: '',
      CUCspcversion: '',
      ONUtype: '',
      Txpowersupplycontrol: '',
      PhaseState: '',
    };
  }

  getIP() {
    const ifaces = os.networkInterfaces();
    for (const dev in ifaces) {
      for (const index in ifaces[dev]) {
        if (ifaces[dev][index].family == 'IPv4') {
          const myip = ifaces[dev][index].address;
          if (myip != '127.0.0.1' && !dev.match(/VMware/)) {
            return myip;
          }
        }
      }
    }
  }
}

module.exports = Olt;
